
				********************************************************************
				* 		Enhanced-Home Food Production Program in Burkina Faso 	   *
				* 																   *
				*		By: Malek Abu-Jawdeh & Lilia Bliznashka 				   *
				*                                         				           *      
				* Last Edited: LB, October 4, 2013 							       *
				* Last Edited: LB, November 21, 2013 							   *
				* STATA Version 13												   *
				********************************************************************
clear all
set more off
/*
*** define globals ***

	di "`c(pwd)'"

	global dir=subinstr(`"`c(pwd)'"',"\do","",.)

	global data2010 `"$dir/data/Round 1 2010"'
	global data2012 `"$dir/data/Round 2 2012"'
	global data2013 `"$dir/data/Round 3 2013"'
	global do `"$dir/do"'
*/
* open clean file *

	use "$data2013/R3_Section_24.dta", clear

	
* merge with cover page to get interview date *

	merge m:1 FPrimary using "$data2013/R3_Section_0.dta", keepusing(day month year intdate)
	keep if _m==3 // 225 from using didn't merge
	drop _m

* rename and relabel vars *	
	
	tab intdate, m nol
	lab var intdate "Interview date" 

	rename day intdate_day
	rename month intdate_month
	rename year intdate_year
	
* double check for duplicates and unique IDs * 		
	isid FPrimary idp_child
		* ok *
		
	duplicates report
		* no duplicates *

* cleaning vars *
	
*** date of birth *** 

	tab1 s24_q3a s24_q3b s24_q3c, m nol
		* 777 missing days, 749 missing months, 692 missing year * 
				
		tab1 s24_q3a s24_q3b s24_q3c, m nol	
			* 29 missing birthdates *
			
		* create DOB * 
			* verify DOB will be correct * 
			 cap assert s24_q3a<=30 if !missing(s24_q3a) & (s24_q3b==4 | s24_q3b==6 | s24_q3b==9 | s24_q3b==11)
				* 1 contradictions *
				*br if s24_q3a>30 & (s24_q3b==4 | s24_q3b==6 | s24_q3b==9 | s24_q3b==11) &  !missing(s24_q3a)
				
				replace s24_q3a=30 if s24_q3b==11 & FPrimary==12202
				
			assert s24_q3a<=31 if !missing(s24_q3a) & (s24_q3b==1 | s24_q3b==3 | s24_q3b==5 | s24_q3b==7 | s24_q3b==8 | s24_q3b==10 | s24_q3b==12)
			assert s24_q3a<=29 if !missing(s24_q3a) & s24_q3b==2
				
		gen birthdate = mdy(s24_q3b, s24_q3a, s24_q3c)
		format birthdate %td
			* 29 with missing days * 
		
		lab var birthdate "Date of birth" 
		
		* merge in birthdate from round 1 to eliminate inconsistencies with ages in r2. LB Nov 21, 2013
		tempfile r3 r1
		
		save `r3', replace 
		
		use "$data2010/R1_Section_24", clear
		do "$do/analysis r1 s24_v1"
		
		sort hhid idp_mother idp_child 
		
		save `r1', replace
		
		use `r3', clear 
		
		merge 1:1 hhid idp_mother idp_child using `r1', keepusing(birthdate) update replace 
		drop if _m==2		
		
		* generate random DOB if day is missing * 
		gen date_born_rand=1 if s24_q3a==. 
		replace date_born_rand=0 if s24_q3a~=. 
		tab date_born_rand
			* ok *
		
		lab var date_born_rand "Child birth date randomly created"
		label values date_born_rand yesno

		* If birth date and interview date have the same month and year, and birth day is missing, generate a birth date that is between 1 and the interview date * 
		* Can end up with age in days = 0 if birth date and measurement date are equal, which could happen even though it's unlikely * 

		gen s24_q3a_new=round(uniform()*intdate_day+1,1) if (s24_q3b==intdate_month & intdate_year==s24_q3c & s24_q3a==.)
		tab s24_q3a_new, m
		replace s24_q3a=s24_q3a_new if (s24_q3b==intdate_month & intdate_year==s24_q3c & s24_q3a==.)

		* If birth date and measurement date are different, just replace the missing birth date with a random integer controlling for the length of the month and leap years *
		replace s24_q3a= round(uniform()*29+1,1) if s24_q3a==. & (s24_q3b==4 | s24_q3b==6 | s24_q3b==9 | s24_q3b==11) 
		replace s24_q3a= round(uniform()*30+1,1) if s24_q3a==. & (s24_q3b==1 | s24_q3b==3 | s24_q3b==5 | s24_q3b==7 | s24_q3b==8 | s24_q3b==10 | s24_q3b==12) 
		replace s24_q3a= round(uniform()*27+1,1) if s24_q3a==. & s24_q3b==2

		tab s24_q3a, m
		
		
		* update DOB *
		replace birthdate = mdy(s24_q3b, s24_q3a, s24_q3c)
		format birthdate %td

		tab birthdate, m		

*** child height *** 
	
	tab s24_q7, m nol
	replace s24_q7=. if (s24_q7<20 | s24_q7>120)
	
*** child weight *** 
	
	replace s24_q5=. if s24_q5<35
	gen ch_weight=s24_q5-s24_q6 
	tab ch_weight, m

	lab var ch_weight "Child's weight (in kg)"
	
*** create age vars ***

	gen ch_ageda=intdate-birthdate
	tab ch_ageda, m
	
	gen ch_agemo=ch_ageda/(365.25/12)
	tab ch_agemo, m
	
	label variable ch_ageda "Child age in days"
	label variable ch_agemo "Child age in months (aged/30.437)"
	
*** rename vars to convention ****

	rename s24_q4 ch_sex
	rename s24_q7 ch_height

	
*** drop unnecessary vars **** 	
	drop s24_q3a s24_q3b s24_q3c s24_q5 s24_q6 intdate_year intdate_month intdate_day s24_q3a_new

	order FPrimary idp_mother idp_child intdate birthdate ch_sex ch_ageda ch_agemo ch_weight ch_height date_born_rand
	
	
*** creating 1, 3, and 6 motnh age groups ***
	
	label drop ch_age6mo ch_age3mo ch_age1mo
	
	* 6 months * 
	egen ch_age6mo=cut(ch_agemo), at(0(6)60) icodes
	replace ch_age6mo=ch_age6mo+1
	label define ch_age6mo 1 "0 to 5.9"
	local counter=2 
	forvalue j=6(6)60 {
		local upper=`j'+6-.1
		lab def ch_age6mo `counter' "`j' to `upper'", add 
		local counter=`counter'+1
	}
	label values ch_age6mo ch_age6mo
	lab var ch_age6mo "Age groups (6 mo) index child" 

	* 3 months *
	egen ch_age3mo=cut(ch_agemo), at(0(3)60) icodes
	replace ch_age3mo=ch_age3mo+1
	label define ch_age3mo 1 "0 to 2.9" 
	local counter=2 
	forvalue j=3(3)60{
		local upper=`j'+3-.1
		lab def ch_age3mo `counter' "`j' to `upper'", add
		local counter=`counter'+1
	}
	label values ch_age3mo ch_age3mo
	lab var ch_age3mo "Age groups (3 mo) index child"
	
	* 1 month *
	egen ch_age1mo=cut(ch_agemo), at(0(1)60) icodes
	replace ch_age1mo=ch_age1mo+1
	lab def ch_age1mo 1 "0 to 0.9"
	local counter=2
	forvalue j=1(1)60 {
		local upper=`j'+1-.1
		lab def ch_age1mo `counter' "`j' to `upper'", add
		local counter=`counter'+1
	}
	lab values ch_age1mo ch_age1mo
	lab var ch_age1mo "Age groups (1 mo) index child" 
	

*** Create Zscores ****
	
	zscore06, a(ch_agemo) h(ch_height) w(ch_weight) s(ch_sex) female(0) male(1)
		
	rename haz06 ch_haz06
	rename waz06 ch_waz06
	rename whz06 ch_whz06
	rename bmiz06 ch_bmi06
	

*** New Vars *** 

*** anemic ***
	tab s24_q9, m
	replace s24_q9=. if s24_q9>20
	
	gen anemic=0 if !missing(s24_q9)
	replace anemic=1 if s24_q9<11
	lab var anemic "Child is anemic" 
		
	gen anemic_severe=0 if !missing(s24_q9)
	replace anemic_severe=1 if s24_q9<7
	lab var anemic_severe "Child is severely anemic"

*** stunting *** 
	tab ch_haz06, m
	
	gen stunted=0 if !missing(ch_haz06)
	replace stunted=1 if ch_haz06<-2
	lab var stunted "Child is stunted" 
	
*** underweight ***
	tab ch_waz06, m 
	
	gen underweight=0 if !missing(ch_waz06)
	replace underweight=1 if ch_waz06<-2
	lab var underweight "Child is underweight"
	
*** wasting *** 	
	tab ch_whz06, m
	
	gen wasting=0 if !missing(ch_whz06)
	replace wasting=1 if ch_whz06<-2
	lab var wasting "Child is wasting"
	

exit
